//
//  OrderDestinationCity.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/6/16.
//

import UIKit

class OrderDestinationCity: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
    /*
     isTargetNetPoint long 0
     companyCode      long 110201
     companyName      NSString 测试网点1
     */
    typealias OrderDestinationCityBlock = (_ cityDic:Dictionary<String, Any>) -> Void
    var orderDestinationBlock : OrderDestinationCityBlock!
    
    var pickerAry:Array<Any>!
    var selectDic:Dictionary<String, String>!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUI() {
        self.addSubview(self.mainView)
        self.mainView.addSubview(self.titleLab);
        self.mainView.addSubview(self.cancelBtn);
        self.mainView.addSubview(self.lineView);
        self.mainView.addSubview(self.branchLab);
        self.mainView.addSubview(self.confirmBtn);

        self.mainView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0)
            make.right.equalTo(self.snp.right).offset(0)
            make.bottom.equalTo(self.snp.bottom).offset(0)
            make.height.equalTo(Constants.ratioIntervalHeighrt * 417)
        }
        
        self.titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(50)
            make.right.equalTo(self.mainView.snp.right).offset(-50)
            make.top.equalTo(self.mainView.snp.top).offset(0)
            make.height.equalTo(52)
        }
        self.cancelBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.titleLab.snp.centerY)
            make.right.equalTo(self.snp.right).offset(-15)
            make.height.equalTo(40)
            make.width.equalTo(40)
        }
        
        self.lineView.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(0)
            make.right.equalTo(self.mainView.snp.right).offset(0)
            make.top.equalTo(self.titleLab.snp.bottom).offset(0)
            make.height.equalTo(1)
        }
        self.branchLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(0)
            make.right.equalTo(self.mainView.snp.right).offset(0)
            make.top.equalTo(self.titleLab.snp.bottom).offset(0)
            make.height.equalTo(54)
        }
        self.confirmBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(60)
            make.right.equalTo(self.mainView.snp.right).offset(-60)
            make.bottom.equalTo(self.titleLab.snp.bottom).offset(-(Constants.xSafeBottomHeight + 10))
            make.height.equalTo(40)
        }
    }
    
    @objc func clickConfimBtn() {
        if (orderDestinationBlock != nil) {
            orderDestinationBlock(selectDic)
        }
    }
    
    @objc func clickCancelBtn() {
        self.removeFromSuperview()
    }
    
    func setPickerAry(tempPickerAry:Array<Any>) {
        pickerAry = tempPickerAry
        self.mainView.addSubview(self.pickerView)
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerAry.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let dic:Dictionary = pickerAry[row] as! Dictionary<String, String>
        return dic["companyName"]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectDic = pickerAry[row] as? Dictionary<String, String>
    }
    
    lazy var mainView: UIView = {
        let mainView = UIView()
        mainView.backgroundColor = UIColor.white
        mainView.layer.masksToBounds = true
        mainView.layer.cornerRadius = 15
        return mainView
    }()
    
    lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        selectDic = pickerAry[0] as? Dictionary<String, String>
        return pickerView
    }()
    
    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), for: .normal)
        confirmBtn.layer.cornerRadius = 20
        confirmBtn.backgroundColor = UIColor.init(hexString: "#FFD10A")
        confirmBtn.addTarget(self, action: #selector(clickConfimBtn), for: .touchUpInside)
        confirmBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        return confirmBtn
    }()
    
    lazy var cancelBtn: UIButton = {
        let cancelBtn = UIButton.init(type: .custom)
        cancelBtn.setImage(UIImage.init(named: "login_cancel"), for: .normal)
        cancelBtn.addTarget(self, action: #selector(clickCancelBtn), for: .touchUpInside)
        return cancelBtn
    }()
    
    lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_formulateDestination)
        titleLab.textColor = UIColor.init(hexString: "#313131")
        titleLab.font = UIFont.systemFont(ofSize: 16)
        titleLab.textAlignment = .center
        return titleLab
    }()
    
    lazy var branchLab: UILabel = {
        let branchLab = UILabel()
        branchLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_branch)
        branchLab.textColor = UIColor.init(hexString: "#313131")
        branchLab.font = UIFont.systemFont(ofSize: 16)
        branchLab.textAlignment = .center
        return branchLab
    }()
    
    lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = UIColor.init(hexString: "#D8D8D8")
        return lineView
    }()
    
}
